#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/10/15 17:13
# @Author  : leon yan
# @Site    :
# @File    : qequipLayout.py
# @Software: PyCharm

import sys
import Globalist as gl

from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QPainter, QPen, QBrush, QFontMetrics, QColor, QPixmap
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget


class QequipLayout(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.resize(1280, 80)
        self._center()
        self.objStop2 = QPixmap("./resource/icons/stop2.png")
        self.layoutCoordinate = []
        self.layoutDict = {}
        self.linkDict = {}
        self.textDict = {}
        self.markerEmergencyDict = {}  # emergency stop
        self.initial()

    def _center(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move(int((screen.width() - size.width()) / 2),
                  int((screen.height() - size.height()) / 2 - 20))

    def initial(self):
        # 机器布置图
        for i in range(26):
            aX = 5 + i*(40+4)
            aY = 25
            self.layoutCoordinate.append([aX, aY])
            aa2 = {i: 0}
            self.layoutDict.update(aa2)

    def setLayoutDict(self, strDict):
        self.layoutDict = strDict
        self.repaint()

    def setTextDict(self, strText, sKey):
        if self.textDict.__contains__(sKey):
            self.textDict[sKey] = strText
        else:
            self.textDict[sKey] = strText
        self.repaint()

    def setMarkerEmergencyDict(self, strDict):
        self.markerEmergencyDict = strDict
        self.repaint()

    def setLinkDict(self, strDict):
        self.linkDict = strDict
        self.repaint()

    def paintEvent(self, event):  # 在窗口上绘图
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setRenderHint(QPainter.TextAntialiasing)

        rect = QRect(0, 0, self.width(), self.height())  # viewport矩形区
        painter.setViewport(rect)  # 设置Viewport
        painter.setWindow(0, 0, 1280, 80)  # 设置窗口大小，逻辑坐标

        # 机器布置图
        pen = QPen()
        pen.setWidth(1)  # 线宽
        pen.setColor(QColor(0, 0, 0))  # 划线颜色
        pen.setStyle(Qt.SolidLine)  # 线的类型，实线、虚线等
        pen.setCapStyle(Qt.FlatCap)  # 线端点样式
        pen.setJoinStyle(Qt.BevelJoin)  # 线的连接点样式
        painter.setPen(pen)

        brush = QBrush()  # 设置画刷
        brush.setStyle(Qt.SolidPattern)  # 画刷填充样式
        painter.setBrush(brush)

        if self.layoutDict:
            i = 0
            for key, value in self.layoutDict.items():
                if value == 0:
                    brush.setColor(QColor(216, 216, 216))  # 画刷颜色
                elif value == 1:
                    brush.setColor(QColor(217, 255, 196))  # 画刷颜色
                elif value == 2:
                    brush.setColor(QColor(255, 222, 213))  # 画刷颜色
                painter.setBrush(brush)
                rect.setRect(self.layoutCoordinate[i][0], self.layoutCoordinate[i][1], 40, 35)
                painter.drawRect(rect)
                i += 1

        ''''''
        painter.setFont(self.font())
        pen.setColor(QColor(52, 132, 17))  # 划线颜色
        painter.setPen(pen)
        textSize = QFontMetrics(self.font())
        # 机器代码标注
        if self.textDict:
            i = 0
            for key, value in self.textDict.items():
                painter.drawText(self.layoutCoordinate[i][0] + 2,
                                 self.layoutCoordinate[i][1] - 10, self.textDict[i])
                i += 1

        # 急停标识
        if self.markerEmergencyDict:
            i = 0
            for key, value in self.markerEmergencyDict.items():
                if value is not None and value != 0:
                    painter.drawPixmap(self.layoutCoordinate[i][0] + 5, self.layoutCoordinate[i][1] + 10, 16, 18, self.objStop2)
                i += 1

        # tcp标识
        if self.linkDict:
            i = 0
            for key, value in self.linkDict.items():
                if value[2]:
                    if value[3] == 1:
                        brush.setColor(QColor(52, 132, 17))  # 画刷颜色
                        painter.setBrush(brush)
                    else:
                        brush.setColor(QColor(226, 226, 226))  # 画刷颜色
                        painter.setBrush(brush)
                    rect.setRect(self.layoutCoordinate[i][0] + 15, self.layoutCoordinate[i][1] + 40, 10, 10)
                    painter.drawRect(rect)
                i += 1


if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = QequipLayout()
    form.show()
    sys.exit(app.exec_())
